import requests
from bs4 import BeautifulSoup
import pymysql

class TypeSpider(object):
    conn = pymysql.connect(
        host='localhost', user='root', password="admin",
        database='python', port=3306,
        charset='utf8'
    )
    cursor = conn.cursor()
    """初始化 传入类型地址"""
    def __init__(self,url,formName="video_type",):
        self.url=url #'http://moe.005.tv/moeimg/tb/list_3_'
        self.sql="insert into "+formName+"(name,href,pic) values(%s,%s,%s)" #"insert into video_type(name,href) values(%s,%s)"
        self.spiderUrl=""

    def start(self,s,e):
        for i in range(s, e):
            self.spiderUrl = self.url + str(i) + '.html'
            self.__analysis(self.__fetch_content())

    def __analysis(self,html):
        soup = BeautifulSoup(html, 'lxml')
        lists=soup.find('div',class_='zhuti_w_list').findAll('strong')
        piclist=soup.find('div',class_='zhuti_w_list').findAll('img')

        a = []
        for i in range(0,len(lists)):
            ls=lists[i]
            pic=piclist[i]
            a.append((ls.get_text('b'), ls.find('a')['href'],pic['src']))
        print(a)
        self.cursor.executemany(self.sql, a)
        self.conn.commit()

    def __fetch_content(self):
        resObj=requests.get(self.spiderUrl)
        resObj.encoding="utf-8"
        html=resObj.text
        return html

if __name__=="__main__":
    typeS=TypeSpider("http://moe.005.tv/zt/10/list_9_","win10_menu_type")
    typeS.start(1,3)




